/*
 * Copyright 2006 Robert Burrell Donkin
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 
package rat.license.standard;

import java.util.regex.Pattern;

import rat.license.ILicense;
import rat.license.ILicenseMatcher;

/**
 * Looks for documents contain the OASIS copyright claim plus derivative work clause.
 * Perhaps need to match more.
 */
public class OASISLicense extends BaseLicense implements ILicenseMatcher, ILicense {

    private static final String COPYRIGHT_PATTERN_DEFN = ".*Copyright.*OASIS Open.*";
    private static final String CLAUSE_DEFN
    = ".*thisdocumentandtranslationsofitmaybecopiedandfurnishedtoothersandderivativeworks" +
            "thatcommentonorotherwiseexplainitorassistinitsimplementationmaybeprepared" +
            "copiedpublishedanddistributed.*";
    
    private static final Pattern COPYRIGHT_PATTERN = Pattern.compile(COPYRIGHT_PATTERN_DEFN);
    private static final Pattern CLAUSE_PATTERN = Pattern.compile(CLAUSE_DEFN);

    boolean copyrightMatch = false;
    final StringBuffer buffer = new StringBuffer();
    
    public OASISLicense() {
        super("OASIS", "", "No modifications allowed", "Open", true);
    }

    public ILicense match(String line) {
        ILicense result = null;
        
        if (copyrightMatch) {
            line = line.toLowerCase();
            buffer.append(line);
            prune(buffer); 
            final boolean clauseMatch = CLAUSE_PATTERN.matcher(buffer).matches();
            if (clauseMatch) {
                result = this;
            }
            
        } else {
            copyrightMatch = COPYRIGHT_PATTERN.matcher(line).matches();
        }
        return result;
    }
    
    private void prune(final StringBuffer buffer) {
        final int length = buffer.length();
        for (int i=length;i>0;) {
            char at = buffer.charAt(--i);
            if (at < 'a' || at > 'z')
            {
                buffer.deleteCharAt(i);
            }
        }
    }

    public void reset() {
        copyrightMatch = false;
        buffer.delete(0, buffer.length());
    }
}
